package com.dut.bbs.activity;

import java.util.List;
import java.util.Map;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.dut.bbs.R;
import com.dut.bbs.adapter.MaillistAdapter;
import com.dut.bbs.data.ParseMails;
import com.dut.bbs.util.Dg;
import com.dut.bbs.util.URLConstant;
import com.dut.bbs.web.HtmlSource;

public class MailActivity extends BaseActivity {
	private ListView artlist;
	private List<Map<String,String>> ItemList;
	private Context context;
    private ImageButton bt_next,bt_prev,bt_ref,bt_reply;
    private int currentPage; //当前页数
	private int maxPage;    //最大页数
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mails);
		context = MailActivity.this;
		artlist = (ListView) findViewById(R.id.lv_maillist);
		artlist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				Intent intent = new Intent(context,
						MailDetailActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("url", ItemList.get(position).get("url"));
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		bt_next = (ImageButton) findViewById(R.id.ib_mailnext);
		bt_prev = (ImageButton) findViewById(R.id.ib_mailprev);
		bt_ref = (ImageButton) findViewById(R.id.ib_mailref);
		bt_reply = (ImageButton) findViewById(R.id.ib_mailreply);
		bt_next.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(currentPage==maxPage)
					Dg.showToast(context, "已经是最后一页了");
				else {
					HtmlSourseGeter h1 = new HtmlSourseGeter(context);
					h1.execute(URLConstant.MAIL_URI+"?p="+(currentPage+1));
					currentPage++;
				}
			}
		});
		bt_prev.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(currentPage==1)
					Dg.showToast(context, "已经是第一页了");
				else{
					HtmlSourseGeter h2 = new HtmlSourseGeter(context);
					h2.execute(URLConstant.MAIL_URI+"?p="+(currentPage-1));
					currentPage--;
				}
			}
		});
		bt_ref.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				HtmlSourseGeter h3 = new HtmlSourseGeter(context);
				h3.execute(URLConstant.MAIL_URI+"?p="+currentPage);
			}
		});
		bt_reply.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context,PostActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("flag", 4);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		HtmlSourseGeter h = new HtmlSourseGeter(context);
		h.execute(URLConstant.MAIL_URI);
	}
	class HtmlSourseGeter extends AsyncTask<String, integer, String>
    {

        private Context context;
        public HtmlSourseGeter(Context context)
        {
            this.context = context;
        }

        @Override
        protected String doInBackground(String... params)
        {
            String result = null;
            try
            {
                result = HtmlSource.getHtml_data(params[0]);
            } catch (Exception e)
            {
            }
            try{
            	currentPage = Integer.parseInt(result.split("页</a>\\|<a class=\"plant\">")[1].split("/")[0]);
            	maxPage = Integer.parseInt(result.split("页</a>\\|<a class=\"plant\">")[1].split("/")[1].split("<")[0]);
            }catch(Exception e){
            	currentPage=1;
            	maxPage=1;
            }
            return result;
        }

        @Override
        protected void onPreExecute()
        {
        	getProgressDialog().setMessage("抓取页面中...");
            getProgressDialog().show();
        }

        @Override
        protected void onPostExecute(String str)
        {

            String date = str;

            if (date == null)
            {
                Dg.showToast(context, "获取邮件列表失败！");
                getProgressDialog().dismiss();
                return;
            }
            try{
    			ItemList = ParseMails.getart(str);
    			MaillistAdapter adapter = new MaillistAdapter(context,ItemList);
                artlist.setAdapter(adapter);
            	getProgressDialog().dismiss();
            }catch(Exception e){
            	getProgressDialog().dismiss();
            	Dg.showToast(context, "抓取失败！");
            }                     
        }

    }
}
